import { _decorator, animation, Animation } from "cc";
import { Entity } from "../Entity";
import { DirectionNames } from "../../enums/Direction";
import { CocosUtils } from "../../utils/CocosUtils";
const { ccclass } = _decorator;

@ccclass("TurnState")
export class TurnState extends animation.StateMachineComponent {

    public async onMotionStateEnter(controller: animation.AnimationController, motionStateStatus: Readonly<animation.MotionStateStatus>): Promise<void> {
        const entity = controller.getComponent(Entity);
        const action = entity.state === Entity.State.TURNLEFT ? "turnleft" : "turnright";
        const delta = entity.state === Entity.State.TURNLEFT ? 1 : -1;
        let direction = entity.direction;
        direction = (direction + delta + 4) % 4;

        const animation = controller.getComponentInChildren(Animation);
        const entityName = entity.name;
        const animName = `${entityName}/${action}/${DirectionNames[direction]}`;
        const atlasPath = `texture/player/${action}/${DirectionNames[direction]}/${action}`;
        await CocosUtils.playAnimation(animation, animName, atlasPath);
        entity.direction = direction;
        entity.state = Entity.State.IDLE;
    }
}